const { WechatOrder } = require('../../schemas/wechatSchema')
const Snowflake = require('../../utils/snowflake')

const snowflake = new Snowflake(1, 1)
const { timeFormat } = require('../../utils')

// 微信 维修 订单 查询
const servicesData = async (request, reply) => {
  const { userPhoneNo, currentPage = 1, pageSize = 10 } = request.query
  if (userPhoneNo) {
    let page = parseInt(currentPage, 10)
    let size = parseInt(pageSize, 10)
    if (page < 1) page = 1
    if (size < 1) size = 10

    const skip = (page - 1) * size

    const userData = await WechatOrder.find(
      { userPhoneNo },
      { _id: 0, __v: 0, userPhoneNo: 0 }
    )
      .sort({ createdAt: -1 })
      .skip(skip)
      .limit(size)
      .exec()
    const total = await WechatOrder.countDocuments({ userPhoneNo }).exec()

    const _userData = userData.map((item) => {
      return {
        ...item._doc,
        createdAt: timeFormat(item.createdAt),
        updatedAt: timeFormat(item.updatedAt),
      }
    })

    return reply.code(200).send({
      code: reply.statusCode,
      message: '',
      data: _userData,
      error: null,
      totalPages: Math.ceil(total / pageSize),
      currentPage: page,
      pageSize: size,
      total,
    })
  }
  return reply.code(404).send({
    code: reply.statusCode,
    message: 'userPhoneNo 不能为空',
    data: null,
    error: 'userPhoneNo 不能为空',
  })
}

// 更新
const updateData = async (request, reply) => {
  const { orderNo } = request.body
  if (!orderNo) {
    const orderNo = snowflake.generateOrderId()
    let newDorder = new WechatOrder({
      ...request.body,
      orderNo: orderNo,
    })

    const addOrder = await newDorder.save()
    if (addOrder) {
      return reply.code(200).send({
        code: reply.statusCode,
        message: '添加成功',
        data: null,
      })
    }
    return reply.code(404).send({
      code: reply.statusCode,
      message: '添加失败！',
      data: null,
    })
  } else {
    try {
      const updateOrder = await WechatOrder.findOneAndUpdate(
        { orderNo: orderNo },
        request.body,
        { new: true }
      )
      return reply.code(200).send({
        code: reply.statusCode,
        message: '更新成功！',
        data: null,
      })
    } catch (error) {
      return reply.code(404).send({
        code: reply.statusCode,
        message: '类型错误！',
        data: null,
        error: error.message,
      })
    }
  }
}

// 删除
const deleteData = async (request, reply) => {
  try {
    const result = await WechatOrder.deleteOne(request.query)
    if (result.acknowledged && result.deletedCount) {
      return reply.code(200).send({
        code: reply.statusCode,
        message: '删除成功！',
        data: null,
      })
    } else {
      return reply.code(404).send({
        code: reply.statusCode,
        message: `删除错误！`,
        data: null,
      })
    }
  } catch (error) {
    return reply.code(404).send({
      code: reply.statusCode,
      message: `删除错误！`,
      data: null,
      error: error.message,
    })
  }
}

// 订单 详情
const detailData = async (request, reply) => {
  const { orderNo } = request.params
  const detailsData = await WechatOrder.findOne(
    { orderNo },
    { _id: 0, __v: 0, userPhoneNo: 0 }
  )
  if (detailsData) {
    return reply.code(200).send({
      code: reply.statusCode,
      message: null,
      data: {
        ...detailsData._doc,
        createdAt: timeFormat(detailsData.createdAt),
        updatedAt: timeFormat(detailsData.updatedAt),
      },
      error: null,
    })
  }
  return reply.code(404).send({
    code: reply.statusCode,
    message: '出错了',
    data: null,
  })
}
module.exports = {
  servicesData,
  updateData,
  deleteData,
  detailData,
}
